/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.spring.initilizer;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.genia.toolbox.basics.bean.EnumerationIterator;

public class DumbServletContext
    implements ServletContext
{

  /**
   * the <code>Log</code> variable.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(DumbServletContext.class);

  private Map<String, Object> attributes = new TreeMap<String, Object>();



  public Object getAttribute(final String name)
  {
    return attributes.get(name);
  }



  public Enumeration<String> getAttributeNames()
  {
    return new EnumerationIterator<String>(attributes.keySet());
  }



  public ServletContext getContext(final String uripath)
  {
    return this;
  }



  public String getInitParameter(final String name)
  {
    return null;
  }



  @SuppressWarnings("unchecked")
  public Enumeration getInitParameterNames()
  {
    return new EnumerationIterator(Collections.emptyList());
  }



  public int getMajorVersion()
  {
    return 2;
  }



  public String getMimeType(final String file)
  {
    return null;
  }



  public int getMinorVersion()
  {
    return 4;
  }



  public RequestDispatcher getNamedDispatcher(final String name)
  {
    return null;
  }



  public String getRealPath(final String path)
  {
    return path;
  }



  public RequestDispatcher getRequestDispatcher(final String path)
  {
    return null;
  }



  public URL getResource(final String path)
      throws MalformedURLException
  {
    return new File(path).toURL();
  }



  public InputStream getResourceAsStream(final String path)
  {
    try {
      return new FileInputStream(path);
    }
    catch (final Exception e) {
      return null;
    }
  }



  public Set<String> getResourcePaths(final String path)
  {
    return null;
  }



  public String getServerInfo()
  {
    return "Dumb Server";
  }



  public Servlet getServlet(final String name)
  {
    return null;
  }



  public String getServletContextName()
  {
    return null;
  }



  @SuppressWarnings("unchecked")
  public Enumeration getServletNames()
  {
    return new EnumerationIterator(Collections.emptyList());
  }



  @SuppressWarnings("unchecked")
  public Enumeration getServlets()
  {
    return new EnumerationIterator(Collections.emptyList());
  }



  public void log(final String msg)
  {
    LOGGER.info(msg);
  }



  public void log(final Exception exception, final String msg)
  {
    LOGGER.error(msg, exception);
  }



  public void log(final String message, final Throwable throwable)
  {
    LOGGER.error(message, throwable);
  }



  public void removeAttribute(final String name)
  {
    attributes.remove(name);
  }



  public void setAttribute(final String name, final Object object)
  {
    attributes.put(name, object);
  }

}
